//
// Created by Administrator on 2021/11/3.
//

#include "Vector2.h"

Vector2::Vector2() {
    x = y = 0.0f;
}

Vector2::~Vector2() {
}

Vector2::Vector2(float v[3]) {
    if(MAssert::isNull(v)){
        return;
    }
    x = v[0]; y = v[1];
}

Vector2::Vector2(float nx, float ny) {
    x = nx; y = ny;
}

Vector2::Vector2(const Vector2 &v) {
    x = v.x; y = v.y;
}

Vector2 &Vector2::operator=(const Vector2 &v) {
    x = v.x;
    y = v.y;
    return *this;
}

bool Vector2::operator==(const Vector2 &v) const{
    return x == v.x && y == v.y;
}

bool Vector2::operator!=(const Vector2 &v) const{
    return x != v.x || y != v.y ;
}

Vector2 Vector2::operator+(const Vector2 &v) const{
    return Vector2(x + v.x, y + v.y);
}

Vector2 Vector2::operator-(const Vector2 &v) const{
    return Vector2(x - v.x, y - v.y);
}

Vector2 Vector2::operator*(float a) const{
    return Vector2(x * a, y * a);
}

Vector2 Vector2::operator/(float a) const{
    float _a = 1 / a;
    return Vector2(x * _a, y * _a);
}

float Vector2::length() {
    return std::sqrt(x * x + y * y);
}

void Vector2::normalize() {
    float len = length();
    x = x / len;
    y = y / len;
}

Vector2 Vector2::zero() {
    return Vector2(0.0f, 0.0f);
}

Vector2 Vector2::identity() {
    return Vector2(1.0f, 1.0f);
}

float Vector2::operator*(const Vector2 &v) const{
    return x * v.x + y * v.y;
}

String Vector2::toString() {
    String str = "(x = " + std::to_string(x) + ", y = " + std::to_string(y) + ")";
    return str;
}

/**
* 向量相减
*/
Vector2 Vector2::subtractVectors(const Vector2& v1, const Vector2& v2) {
    float _x = v1.x - v2.x;
    float _y = v1.y - v2.y;
    return Vector2(_x, _y);
}

/**
 * 计算两个向量的夹角 ||a||||b||cosθ = a·b
 * @param v1 向量1
 * @param v2 向量2
 * @return 角度
 */
float Vector2::angle(Vector2 v1, Vector2 v2, AngleType angleType) {
    float mlen = v1.length() * v2.length();
    float rad = std::acos(v1 * v2 * (1 / mlen));
    if(AngleType::DEGREE == angleType){
        return Angle::rad2deg(rad);
    }
    return rad;
}

/**
 * 标量左乘
 * @param f 标量
 * @param v2 向量
 * @return 结果
 */
Vector2 Vector2::multiply(float f, Vector2 v2) {
    return Vector2(v2.x * f, v2.y * f);
}

/**
 * 点乘
 * @param v1 向量1
 * @param v2 向量2
 * @return 点乘结果
 */
float Vector2::multiply(Vector2 v1, Vector2 v2) {
    return v1 * v2;
}

/**
 * 两点间距离
 * @param v1 向量1
 * @param v2 向量2
 * @return 距离
 */
float Vector2::distance(Vector2 v1, Vector2 v2) {
    float dx = v1.x - v2.x;
    float dy = v1.y - v2.y;
    return std::sqrt(dx * dx + dy * dy);
}




